import pygame
from settings import Settings
class Ship:
    # 管理飞船类
    def __init__(self, ai_game):
        # 初始化飞船并设置其位置
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # 实例化设置类
        self.settings = Settings()

        # 飞船模型并获取其矩形大小
        self.image = pygame.image.load('images/ship.png')
        self.image = pygame.transform.scale(self.image, (self.settings.ship_width, self.settings.ship_height))
        self.rect = self.image.get_rect()

        # 新飞船出现的位置
        self.rect.midbottom = self.screen_rect.midbottom
        self.ship_speed = self.settings.ship_speed
        # 初始化移动变量
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        # 记录飞船位置
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

    def blitme(self):
        #在指定位置绘制飞船
        self.screen.blit(self.image, self.rect)

    def update(self):
        #飞船根据移动变量进行移动
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.ship_speed
        if self.moving_up and self.rect.top > 0:
            self.y -= self.ship_speed
        if self.moving_down and self.rect.bottom < 800:
            self.y += self.ship_speed
        # 根据self.x更新位置
        self.rect.x = self.x
        self.rect.y = self.y